﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;//////////////////////

namespace CPF_experement
{
    class SuperTree
    {
        //static public int numOfFindPath;///////////
        //static public Stopwatch watchCreateMdd;///////////
        //static public Stopwatch watchSyncronize;//////////
        //static public Stopwatch watchBruteForce;//////////
        LinkedList<CostsSet> openList;
        HashSet<CostsSet> closedList;
        public int totalCost;
        public int expanded;
        AgentState[] startingPos;

        public SuperTree(AgentState[] startingPos)
        {
            //numOfFindPath = 0;////////////////
            expanded = 0;///////////////////
           // watchCreateMdd = new Stopwatch();//////////////
           // watchSyncronize = new Stopwatch();//////////////
           // watchBruteForce = new Stopwatch();//////////////
            totalCost = -2;
            this.startingPos = startingPos;
            closedList = new HashSet<CostsSet>();
            openList = new LinkedList<CostsSet>();
            int[] costs = new int[startingPos.Length];
            for (int i = 0; i < startingPos.Length; i++)
            {
                costs[i] = startingPos[i].huristic;
            }
          //  watchCreateMdd.Start();
            openList.AddFirst(new CostsSet(costs));
          //  watchCreateMdd.Stop();
        }

        public LinkedList<PoinForNewAlgoritem>[] solve()
        {
            //int counter = 0;
            SuperTreeNode next;
            LinkedList<PoinForNewAlgoritem>[] ans = null;
            while (Run.watch.ElapsedMilliseconds<300000)
            {
                //if (counter==66)
                //{
                //    openList.First.Value.printAllGDDs();
                //}
                //if (openList.First.Value.costs[0] == 0 && openList.First.Value.costs[1] == 4 && openList.First.Value.costs[2] == 4 && openList.First.Value.costs[3] == 4 && openList.First.Value.costs[4] == 2 && openList.First.Value.costs[5] == 1 && openList.First.Value.costs[6] == 4 && openList.First.Value.costs[7] == 4)
                //{
                //    Console.WriteLine("Thghghghsss ");
                //   // openList.First.Value.printAllGDDs();
                //}
                //if (openList.First.Value.costs.Sum()==15)
                //{
                //    Console.WriteLine("Thghghghsss ");
                //}
              //  watchCreateMdd.Start();
                next = new SuperTreeNode(startingPos, openList.First.Value);
              //  watchCreateMdd.Stop();
                expanded++;
                ans = next.solve();
                if (ans!=null)
                {
                    totalCost=0;
                    for (int i = 0; i < next.costs.Length; i++)
			        {
                        totalCost+=next.costs[i];
                       // Console.WriteLine("Total cost - for " + i + "  " + openList.First.Value.costs[i]);
			        }
                    //Console.WriteLine("Total cost - " + totalCost);
                    return ans;
                }

                next.expandNode(openList, closedList);
                openList.RemoveFirst();
                //counter++;
                //Console.WriteLine(counter);
            }
            return ans;
        }
    }
}
